﻿angular.module('app')
    .service('WorkContextSrv', ['$rootScope', '$location', '$timeout', 'webStorage',
        function ( $rootScope, $location, $timeout, webStorage) {
            var tm;
            return {
                isAuthenticated: function () {
                    //if (typeof ipCookie("user") != 'undefined') {
                    //    try {
                    //        var user = angular.fromJson(ipCookie("user"));
                    //        if (typeof user.userName != 'undefined' && user.userName != '') {
                    //            return true;
                    //        }
                    //        return false;
                    //    } catch (e) {
                    //        return false;
                    //    }
                    //}
                    //return false;
                    return webStorage.get("user") != null;
                },
                getCurrentUser: function () {
                    if (webStorage.get("user") != null) {
                        return webStorage.get("user");
                    }
                    //console.log($location);
                    $timeout.cancel(tm);
                    tm = $timeout(function () {
                        $rootScope.$broadcast("redirectToSignInPage");
                    }, 500);
                    return {};
                },
                setCurrentUser: function (currentUser) {
                    webStorage.remove("user");
                    webStorage.add("user", currentUser);
                    //ipCookie("user", angular.toJson(currentuser), {expirationUnit: 'minutes', expires: 30 });
                }
            };
        }
    ]);